<template>
  <!-- 位置信息表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="700"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="位置编号" prop="locationCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.locationCode"
              placeholder="请输入位置编号"
              clearable
            />

            <span v-else>{{ formData.locationCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="位置名称" prop="locationName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.locationName"
              placeholder="请输入位置名称"
              clearable
            />

            <span v-else>{{ formData.locationName }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="位置类型" prop="locationType">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.locationType"
              placeholder="请选择位置类型"
              clearable
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('LocationType')"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>

            <span v-else>{{
              useDictionary("LocationType", formData.locationType)
            }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="x坐标" prop="locationX">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.locationX"
              placeholder="请输入x坐标"
              clearable
            />

            <span v-else>{{ formData.locationX }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input-number
              v-if="!props.isReadonly"
              v-model="formData.sort"
              :min="0"
              placeholder="请输入显示序号"
              style="width: 300px"
              clearable
            />

            <span v-else>{{ formData.sort }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="y坐标" prop="locationY">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.locationY"
              placeholder="请输入y坐标"
              clearable
            />

            <span v-else>{{ formData.locationY }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="启用状态" prop="enabled">
            <el-radio-group v-model="formData.enabled" v-if="!props.isReadonly">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>

            <span v-else>{{ formData.enabled == 1 ? "启用" : "停用" }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="z坐标" prop="locationZ">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.locationZ"
              placeholder="请输入z坐标"
              clearable
            />

            <span v-else>{{ formData.locationZ }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.remarks"
              placeholder="请输入备注"
              type="textarea"
              clearable
            />

            <span v-else>{{ formData.remarks }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="创建时间"
            prop="createTime"
            v-if="props.isReadonly"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createTime"
              disabled
            />

            <span v-else>{{ formData.createTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="创建人"
            prop="createdUserName"
            v-if="props.isReadonly"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createdUserName"
              disabled
            />

            <span v-else>{{ formData.createdUserName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="修改时间"
            prop="lastUpdateTime"
            v-if="props.isReadonly"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdateTime"
              disabled
            />

            <span v-else>{{ formData.lastUpdateTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="修改人"
            prop="lastUpdatedUserName"
            v-if="props.isReadonly"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdatedUserName"
              disabled
            />

            <span v-else>{{ formData.lastUpdatedUserName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();
import { useDictionary } from "@/utils/useTable";
defineOptions({
  name: "MDMMDMLOCATIONINFOADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  parentId: {
    type: String,
    default: () => {
      return "";
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  locationCode: [
    { required: true, message: "请输入位置编号", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  locationName: [
    { required: true, message: "请输入位置名称", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],

  locationX: [
    {
      // required: true,
      pattern: /^\d{1,4}(\.\d{0,2})?$/,
      message: "四位整数,两位小数",
      trigger: "blur",
    },
  ],
  locationY: [
    {
      // required: true,
      pattern: /^\d{1,4}(\.\d{0,2})?$/,
      message: "四位整数,两位小数",
      trigger: "blur",
    },
  ],
  locationZ: [
    {
      // required: true,
      pattern: /^\d{1,4}(\.\d{0,2})?$/,
      message: "四位整数,两位小数",
      trigger: "blur",
    },
  ],
  sort: [{ required: true, message: "请输入显示序号", trigger: "blur" }],
  enabled: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  locationCode: "",
  locationName: "",

  locationType: "",
  locationX: "",
  locationY: "",
  locationZ: "",
  sort: 0,
  enabled: 1,
  parentId: "",
  allId: "",
  allCode: "",
  allName: "",
  remarks: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        locationCode,
        locationName,
        locationType,
        locationX,
        locationY,
        locationZ,
        sort,
        enabled,
        parentId,
        allId,
        allCode,
        allName,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        locationCode,
        locationName,
        locationType,
        locationX,
        locationY,
        locationZ,
        sort,
        enabled,
        parentId,
        allId,
        allCode,
        allName,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.locationCode = "";
  formData.locationName = "";
  formData.locationType = "";
  formData.locationX = "";
  formData.locationY = "";
  formData.locationZ = "";
  formData.sort = 0;
  formData.enabled = 1;
  formData.parentId = "";
  formData.allId = "";
  formData.allCode = "";
  formData.allName = "";
  formData.remarks = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

const parentIdDialog = ref("");
watch(
  () => props.parentId,
  (newVal: string) => {
    parentIdDialog.value = newVal;
  }
);
const isReadonlyDialog = ref(false);
watch(
  () => props.isReadonly,
  (newVal: boolean) => {
    isReadonlyDialog.value = newVal;
  }
);
/** 位置信息保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      // 添加子机构
      const text = ref("");
      if (parentIdDialog.value) {
        formData.parentId = parentIdDialog.value;
        loading.value = true;
        text.value = "添加子位置";
      } else if (formData.id) {
        loading.value = true;
        text.value = "编辑";
      } else {
        loading.value = true;
        formData.parentId = "0";
        text.value = "新增";
      }
      loading.value = false;
      // isReadonlyDialog.value = false;
      await request({
        url: "/eam/locationinfo/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text.value + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
